#pragma once
#include <mysql.h>
using namespace std;

namespace CT
{
    class Client
    {
    public:
        Client(const string &name, const string &password, const string &db, bool *cursituation, const string &ip)
            : _user_name(name), _password(password), _db(db), _clientip(ip)
        {
            // 初始化登录信息
            cout << name << password << db << endl;
            conn = mysql_init(NULL);

            if (!mysql_real_connect(conn, _clientip.c_str(), "hyk", password.c_str(), db.c_str(), 0, NULL, 0))
            {
                cout << "connect error~" << endl;
                *cursituation = false;
            }
            else{
                mysql_set_character_set(conn, "utf8");
            }
        }

        // 借书
        void BorrowBook(/*string book_name, int book_id = -1*/)
        {
            string bookname;
            MYSQL_RES *res;
            // int book_id;   默认书名不重复
            cout << "请输入你想借的书" << endl;
            cin >> bookname;
            string req_sql = "SELECT * FROM books WHERE title='" + bookname + "';";
            int n = mysql_query(conn, req_sql.c_str());
            if (n != 0)
            {
                printf("query error ~\n");
                exit(EXIT_FAILURE);
            }
            res = mysql_store_result(conn);
            if (mysql_num_rows(res) == 0)
            {
                cout << "没有找到这本书。" << endl;
            }
            else
            {
                cout << "找到了，可以借" << endl;
                // 加入该客户的借书表
                // 调用储存过程
                string sql = "CALL BorrowBook(1, 1, @borrow_due_date);";
                n = mysql_query(conn, sql.c_str());
                if (n != 0)
                {
                    printf("query error ~\n");
                    exit(EXIT_FAILURE);
                }
            }
            // 释放结果集
            mysql_free_result(res);
        }
        // 还书
        void ReturnBook(/*string book_name, int book_id = -1*/)
        {
            // string bookname;
            // MYSQL_RES *res;
            // // int book_id;   默认书名不重复
            // cout << "请输入你想还的书" << endl;
            // getline(cin, bookname);

            // string sql = "call ReturnBook(book_id);";
            // int n = mysql_query(conn, sql.c_str());
            // if (n != 0)
            // {
            //     printf("query error ~\n");
            //     exit(EXIT_FAILURE);
            // }

            // // 释放结果集
            // mysql_free_result(res);
            string bookname;
            MYSQL_RES *res;
            MYSQL_ROW row;

            cout << "请输入你想还的书名：" << endl;
            getline(cin, bookname);

            // 假设您有一个根据书名检索书ID的查询
            string searchSql = "SELECT book_id FROM books WHERE bookname='" + bookname + "';";

            int n = mysql_query(conn, searchSql.c_str());
            if (n != 0)
            {
                printf("查询书名错误：%s\n", mysql_error(conn));
                exit(EXIT_FAILURE);
            }

            res = mysql_store_result(conn);
            if (res == NULL)
            {
                printf("存储结果失败：%s\n", mysql_error(conn));
                exit(EXIT_FAILURE);
            }

            if (mysql_num_rows(res) == 1)
            {
                row = mysql_fetch_row(res);
                if (row != NULL)
                {
                    int book_id = atoi(row[0]);

                    // 调用存储过程还书
                    string returnSql = "CALL ReturnBookByID();"; // 使用占位符，实际使用时可能需要预处理语句


                    mysql_free_result(res);
                }
                else
                {
                    printf("无法获取书ID\n");
                }
            }
            else if (mysql_num_rows(res) == 0)
            {
                printf("未找到该书\n");
            }
            mysql_free_result(res);
        }
        void ShowDetails()
        {
            string query = "select * from books";
            int n = mysql_query(conn, query.c_str());
            if (n != 0)
            {
                printf("query error ~\n");
                exit(EXIT_FAILURE);
            }
            MYSQL_RES *res = mysql_store_result(conn);
            int fields = mysql_num_fields(res);
            MYSQL_FIELD *field = mysql_fetch_fields(res);
            int i = 0;
            for (; i < fields; i++)
            {
                cout << field[i].name << " ";
            }
            cout << endl;
            i = 0;
            MYSQL_ROW line;
            for (; i < 6; i++)
            {
                line = mysql_fetch_row(res);
                int j = 0;
                for (; j < fields; j++)
                {
                    cout << line[j] << " ";
                }
                cout << endl;
            }
            mysql_free_result(res);
        }
        void Quit()
        {
            if (conn != nullptr)
            {
                // 关闭连接
                mysql_close(conn);
                // 将指针设置为nullptr，防止后续误用
                conn = nullptr;
                delete conn;
            }
        }

    private:
        // bool isdirect;  //初始化是否成功
        int _user_id;
        string _user_name;
        string _password;
        string _db; // database
        string _clientip;
        MYSQL *conn; // 拷贝数据库的数据结构
    };
}